$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'aic/ctocorder/list',
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'id', index: 'id', width: 50, key: true, hidden: true },
			{ label: '订单号', name: 'orderNo', index: 'order_no', width: 80 }, 			
			{ label: '手机号', name: 'mobile', index: 'mobile', width: 80 },
			{ label: '单价', name: 'coinPrice', index: 'coin_price', width: 80 }, 			
			{ label: '总价', name: 'coinTotalPrice', index: 'coin_total_price', width: 80 }, 			
			{ label: '数量', name: 'coinQuantity', index: 'coin_quantity', width: 80 }, 			
			{ label: '订单描述', name: 'orderDescription', index: 'order_description', width: 80 }, 			
			{ label: '创建时间', name: 'createTime', index: 'create_time', width: 80 }		
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		q: {status: 0},
		showList: true,
		title: null,
		ctocOrder: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增买单";
			vm.ctocOrder = {coinQuantity: 10, coinTotalPrice: 0};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改订单";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
			var url = vm.ctocOrder.id == null ? "aic/ctocorder/save" : "aic/ctocorder/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.ctocOrder),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		cancelOrder: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要取消选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "aic/ctocorder/cancelOrder",
                    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get(baseURL + "aic/ctocorder/info/"+id, function(r){
                vm.ctocOrder = r.ctocOrder;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
				postData:vm.q,
                page:page
            }).trigger("reloadGrid");
		},
		sum: function () {
			var m = 0,
				s1 = vm.ctocOrder.coinPrice.toString(),
				s2 = vm.ctocOrder.coinQuantity.toString();
			try {
				m += s1.split(".")[1].length;
			} catch(e) {}
			try {
				m += s2.split(".")[1].length;
			} catch(e) {}
			vm.ctocOrder.coinTotalPrice = Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m);
		}
	}
});